package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.acticle.service.ISaleService;
import cn.tedu.boyou.acticle.service.ISaleTypeService;
import cn.tedu.boyou.common.query.QueryObject;
import cn.tedu.boyou.search.query.SaleQueryObject;
import cn.tedu.boyou.search.service.ISaleSearchService;
import cn.tedu.boyou.search.service.ISaleTypeSearchService;
import cn.tedu.boyou.search.template.SaleTemplate;
import cn.tedu.boyou.search.template.SaleTypeTemplate;
import cn.tedu.boyou.website.util.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sale")
public class SaleController {

    @Reference
    private ISaleSearchService saleSearchService;

    @Reference
    private ISaleTypeSearchService saleTypeSearchService;

    @RequestMapping("")
    public String sale(Model model){

        return "sale/sale";
    }

    @RequestMapping("/querySale")
    public String query(Model model,@ModelAttribute("qo")SaleQueryObject qo){
        List<SaleTemplate> list = saleSearchService.query(qo);
        model.addAttribute("sales",list);
        return "sale/mainTpl";
    }

    @RequestMapping("/queryPage")
    public String queryPage(Model model, @ModelAttribute("qo")SaleQueryObject qo, String[] monthArray){
        if(monthArray!= null && monthArray.length > 0){
            Date[] dates = new Date[monthArray.length];
            for (int i = 0 ; i < monthArray.length; i++) {
                Date date = DateUtil.str2Date(monthArray[i], "yyyyMM");
                dates[i] = date;
            }
            qo.setState_schedule(dates);
        }
        Page<SaleTemplate> page = saleSearchService.queryPage(qo);
        model.addAttribute("page",page);
        return "sale/mainTpl";
    }

    @RequestMapping("/getSaleType")
    @ResponseBody
    public Object getSaleType(){
        List<SaleTypeTemplate> saleTypeTemplates = saleTypeSearchService.listAll();
        return saleTypeTemplates;
    }
}
